package com.wwwarehouse.carddesk.fragment.functionlogincontrol;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.adapter.functionlogincontrol.FunctionLoginControlAdapter;
import com.wwwarehouse.carddesk.bean.CardDeskGroupresponseBean;
import com.wwwarehouse.carddesk.constant.CardDeskConstant;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.DialogTools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


/**
 * 团队框架 ---- 暂停开启登录
 * minifly
 * 2017:11:15 9:30
 *
 * @author minfily
 */

public class FunctionLoginControlSearchresultFragment extends BaseFragment {

    private View mRootView;
    private RecyclerView mGroupLoginControlSearchresultRecycleview;
    private StateLayout mGroupLoginControlSearchresultStatelayout;
    private FunctionLoginControlAdapter adapter;
    private FunctionLoginControlAdapter.StateOnclickListener listener;
    private int page, pageSize = 7;
    private String buId;
    private String name;
    public static final int REQUEST_ALLPEOPLE = 10004, REQUEST_ALLPEOPLE_STOP = 10005;
    private String type;
    private String personId;
    private String name1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_group_login_control_searchresult, container, false);
        initView(mRootView);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void setTitle() {
        mActivity.setTitle(getString(R.string.teamdesk_login_control));
    }

    @Override
    public void requestDatas() {
        getAllPeople();
    }

    private void initView(View mRootView) {

        mGroupLoginControlSearchresultRecycleview = (RecyclerView) mRootView.findViewById(R.id.group_login_control_searchresult_recycleview);
        mGroupLoginControlSearchresultStatelayout = (StateLayout) mRootView.findViewById(R.id.group_login_control_searchresult_statelayout);

        mGroupLoginControlSearchresultRecycleview.setLayoutManager(new LinearLayoutManager(mActivity));
        listener = new FunctionLoginControlAdapter.StateOnclickListener() {
            @Override
            public void click(String personid, String type1, String name) {
                personId = personid;
                type = type1;
                name1 = name;
                showDialog();
            }
        };


        page = getArguments().getInt("page");
        buId = getArguments().getString("buId");
        name = getArguments().getString("name");
    }

    public void getAllPeople() {
        Map<String, Object> map = new HashMap<>();
        /**
         * buId	业务单位	number	@mock=260001
         name	名字	string	@mock=
         page	页	number
         size	一页大小	number
         */
        map.put("buId", buId);
        map.put("name", "" + name);
        map.put("page", page);
        map.put("size", pageSize);
        NoHttpUtils.httpPost(CardDeskConstant.PATH_CARDDESK_GROUP_STOPLOGINGETALLPEPLE, map, myOnresponse, REQUEST_ALLPEOPLE);
        showProgressDialog();
    }

    public void stopLogin() {
        /**
         * buId	业务单位	number	@mock=260001
         pauseUserId	被操作的用户	number	@mock=100188
         type	开启：1、暂停：0	number	@mock=1
         */
        Map<String, Object> map = new HashMap<>();
        /**
         * buId	业务单位	number	@mock=260001
         name	名字	string	@mock=
         page	页	number
         size	一页大小	number
         */
        map.put("buId", buId);
        map.put("pauseUserId", "" + personId);
        map.put("type", type);
        NoHttpUtils.httpPost(CardDeskConstant.PATH_CARDDESK_GROUP_STOPLOGIN_STOP, map, myOnresponse, REQUEST_ALLPEOPLE_STOP);
        showProgressDialog();
    }

    NoHttpUtils.OnResponseListener myOnresponse = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_ALLPEOPLE:
                    if ("0".equals(commonClass.getCode())) {

                        CardDeskGroupresponseBean bean = JSON.parseObject(commonClass.getData().toString(), CardDeskGroupresponseBean.class);
                        ArrayList<CardDeskGroupresponseBean.ArrayListBean> list = bean.getList();
                        adapter = new FunctionLoginControlAdapter(mActivity, listener, list);
                        mGroupLoginControlSearchresultRecycleview.setAdapter(adapter);

                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                case REQUEST_ALLPEOPLE_STOP:
                    if ("0".equals(commonClass.getCode())) {

                        Bundle bundle = new Bundle();
                        bundle.putInt("total", 0);
                        bundle.putParcelable("bean", null);
                        bundle.putString("buId", buId);
                        bundle.putString("name", name);
                        bundle.putString("type", type);
                        FunctionLoginControlSuccessFragment fragment = new FunctionLoginControlSuccessFragment();
                        fragment.setArguments(bundle);
                        popFragment();
                        pushFragment(fragment);
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                default:

                    break;
            }

        }

        @Override
        public void onFailed(String result, int what) {

        }

        @Override
        public void onFinish(int what) {
            dismissProgressDialog();
        }
    };

    private void showDialog() {
        String stateString = getString(R.string.teamdesk_login_control_stoplogin);
        //开启：1、暂停：0
        if ("0".equals(type + "")) {
            stateString = getString(R.string.teamdesk_login_control_stoplogin);
        } else {
            stateString = getString(R.string.teamdesk_login_control_login);
        }

        String name = String.format(getString(R.string.teamdesk_login_control_stoplogin_promote),name1,name1);
        DialogTools.getInstance().showCustomDialogPrompt(mActivity,
                "" + stateString,
                "" + name,
                false,
                new DialogTools.ConfirmListener() {
                    @Override
                    public void setConfirmListener(Dialog dialog, View view, String s) {
                        //确定
                        stopLogin();
                        dialog.dismiss();
                    }
                }, "" + stateString, "" + getString(R.string.teamdesk_login_control_stoplogin_thinkagain));
    }
}
